/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.easoftware.code.mb;

import java.io.Serializable;
import java.util.logging.Level;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import net.easoftware.code.beans.BeanHelper;
import net.easoftware.code.error.EaError;
import net.easoftware.code.mb.annotation.GenericMaintenanceConfig;
import net.easoftware.code.util.FacesUtil;
import org.apache.log4j.Logger;

/**
 *
 * @author eliu
 */
@Named
@SessionScoped
public class MbGenericMaintenance implements Serializable {

    private final Logger log = Logger.getLogger(MbGenericMaintenance.class);
    private String gridPage;
    private String formPage;
    private GenericMaintenanceWrapper wrapper;
    private boolean actionNew = true;
    private boolean actionDelete = false;
    private boolean actionSave = false;
    private boolean actionCancel = false;
    private boolean actionUpdate = false;
    private boolean actionCustom1 = false;
    private boolean actionCustom2 = false;
    private boolean enableHeader = false;
    private String[] actions;
    private String title;
    private String labelActionCustom1;
    private String labelActionCustom2;

    public String getLabelActionCustom1() {
        if (actionCustom1) {
            labelActionCustom1 = wrapper.getActionCustom1Label();
        }
        return labelActionCustom1;
    }

    public void setLabelActionCustom1(String labelActionCustom1) {
        this.labelActionCustom1 = labelActionCustom1;
    }

    public String getLabelActionCustom2() {
        if (actionCustom2) {
            labelActionCustom2 = wrapper.getActionCustom2Label();
        }
        return this.labelActionCustom2;
    }

    public void setLabelActionCustom2(String labelActionCustom2) {
        this.labelActionCustom2 = labelActionCustom2;
    }

    public boolean isActionCustom1() {
        if (this.wrapper != null) {
            this.actionCustom1 = this.wrapper.isEnabledActionCustom1();
        }
        return actionCustom1;
    }

    public void setActionCustom1(boolean actionCustom1) {
        this.actionCustom1 = actionCustom1;
    }

    public boolean isActionCustom2() {
        if (this.wrapper != null) {
            this.actionCustom2 = this.wrapper.isEnabledActionCustom2();
        }
        return actionCustom2;
    }

    public void setActionCustom2(boolean actionCustom2) {
        this.actionCustom2 = actionCustom2;
    }

    public boolean isActionUpdate() {
        return actionUpdate;
    }

    public void setActionUpdate(boolean actionUpdate) {
        this.actionUpdate = actionUpdate;
    }

    public boolean isActionCancel() {
        return actionCancel;
    }

    public void setActionCancel(boolean actionCancel) {
        this.actionCancel = actionCancel;
    }

    public boolean isEnableHeader() {
        return enableHeader;
    }

    public void setEnableHeader(boolean enableHeader) {
        this.enableHeader = enableHeader;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    private void setDefault() {
        this.setActionNew(true);
        this.setEnableHeader(false);
        this.setActionDelete(false);
        this.setActionCancel(false);
        this.setActionSave(false);
        this.setActionUpdate(false);
    }

    private void actionNewConfig() {
        this.setActionNew(false);
        this.setActionSave(true);
        this.setActionDelete(false);
        this.setActionCancel(true);
        this.setEnableHeader(true);
    }

    private void actionSelectConfig() {
        this.setActionNew(false);
        this.setActionSave(false);
        this.setActionUpdate(true);
        this.setActionDelete(true);
        this.setActionCancel(true);
        this.setEnableHeader(true);
    }

    public void init(String maintenance) {
        try {
            Class c = Class.forName("net.easoftware.code.mb." + maintenance);
            GenericMaintenanceConfig conf = (GenericMaintenanceConfig) c.getAnnotation(GenericMaintenanceConfig.class);
            String name = maintenance.toLowerCase().charAt(0) + maintenance.substring(1);
            GenericMaintenanceWrapper wmb = (GenericMaintenanceWrapper) BeanHelper.manualInject(name);
            this.setWrapper(wmb);
            this.setTitle(conf.title());
            this.setActions(conf.actions());
            this.setGridPage(wmb.getGridPage());
            this.setFormPage(wmb.getFormPage());
        }
        catch (ClassNotFoundException ex) {
            Logger.getLogger(MbGenericMaintenance.class.getName()).error("Error", ex);
        }
    }

    public void actionCancel() {
        setDefault();
    }

    public void actionSelect() {
        try {
            this.actionSelectConfig();
            wrapper.actionSelect();
        }
        catch (EaError ex) {
            java.util.logging.Logger.getLogger(MbGenericMaintenance.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void actionNew() {
        try {
            wrapper.actionNew();
            actionNewConfig();
        }
        catch (EaError ex) {
            Logger.getLogger(MbGenericMaintenance.class.getName()).error("Error", ex);
            FacesUtil.addMessagesError(ex.getMessage());
        }
    }

    public void actionSave() {
        try {
            wrapper.actionSave();
            setDefault();
        }
        catch (EaError ex) {
            Logger.getLogger(MbGenericMaintenance.class.getName()).error("Error", ex);
            FacesUtil.addMessagesError(ex.getMessage());
        }
    }

    public void actionUpdate() {
        try {
            wrapper.actionUpdate();
            setDefault();
        }
        catch (EaError ex) {
            Logger.getLogger(MbGenericMaintenance.class.getName()).error("Error", ex);
            FacesUtil.addMessagesError(ex.getMessage());
        }
    }

    public void actionDelete() {
        try {
            wrapper.actionDelete();
            setDefault();
        }
        catch (EaError ex) {
            Logger.getLogger(MbGenericMaintenance.class.getName()).error("Error", ex);
            FacesUtil.addMessagesError(ex.getMessage());
        }
    }

    public void doActionCustom1() {
        try {
            wrapper.actionCustom1();
            setDefault();
        }
        catch (EaError ex) {
            Logger.getLogger(MbGenericMaintenance.class.getName()).error("Error", ex);
            FacesUtil.addMessagesError(ex.getMessage());
        }
    }

    public void doActionCustom2() {
        try {
            wrapper.actionCustom2();
            setDefault();
        }
        catch (EaError ex) {
            Logger.getLogger(MbGenericMaintenance.class.getName()).error("Error", ex);
            FacesUtil.addMessagesError(ex.getMessage());
        }
    }

    public String getGridPage() {
        return gridPage;
    }

    public void setGridPage(String gridPage) {
        this.gridPage = gridPage;
    }

    public String getFormPage() {
        return formPage;
    }

    public void setFormPage(String formPage) {
        this.formPage = formPage;
    }

    public GenericMaintenanceWrapper getWrapper() {
        return wrapper;
    }

    public void setWrapper(GenericMaintenanceWrapper wrapper) {
        this.wrapper = wrapper;
    }

    public boolean isActionNew() {
        return actionNew;
    }

    public void setActionNew(boolean actionNew) {
        this.actionNew = actionNew;
    }

    public boolean isActionDelete() {
        return actionDelete;
    }

    public void setActionDelete(boolean actionDelete) {
        this.actionDelete = actionDelete;
    }

    public boolean isActionSave() {
        return actionSave;
    }

    public void setActionSave(boolean actionSave) {
        this.actionSave = actionSave;
    }

    public String[] getActions() {
        return actions;
    }

    public void setActions(String[] actions) {
        this.actions = actions;
    }
}
